﻿using Donal.Xms.Common;
using Newtonsoft.Json;
using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class company_edit : Web.UI.ManagePage
    {
        protected string p = string.Empty;
        protected string c = string.Empty;
        protected string a = string.Empty;

        protected string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        protected int id = 0;
        protected int pageindex = 0;
        protected int page = 0;
        protected int pageSize;
        protected int totalCount = 0;
        private Model.company _company = null;

        private int _cashFrom = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetAdminInfo().role_id > 1)
            {                
                txtAmount.Enabled = false;
                txtOrderLimit.Enabled = false;
            }

            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                this.pageindex = DTRequest.GetQueryInt("pageindex");

                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.company().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("user_company", DTEnums.ActionEnum.View.ToString()); //检查权限

                TreeBind("is_lock=0"); //绑定组别
                IndustryBind("is_lock=0");

                this._cashFrom = DTRequest.GetQueryInt("cashFrom");


                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);

                    switch (pageindex)
                    {
                        case 1:
                            addressRptBind();
                            break;
                        case 2:
                            userRptBind();
                            break;
                        case 3:
                            this.pageSize = GetPageSizeInvoice(10); //每页数量
                            invoiceRptBind();
                            break;
                        case 4:
                            this.pageSize = GetPageSize(10); //每页数量
                            recodeRptBind();
                            break;
                        case 5:
                            invRptBind();
                            break;
                    }

                }
            }
        }

        #region 绑定行业=================================
        private void IndustryBind(string strWhere)
        {
            BLL.user_industry bll = new BLL.user_industry();
            DataTable dt = bll.GetList(0, strWhere, "sort_id,id desc").Tables[0];

            this.ddlIndustry.Items.Clear();
            this.ddlIndustry.Items.Add(new ListItem("会员行业", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlIndustry.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion

        #region 绑定组别=================================
        private void TreeBind(string strWhere)
        {
            BLL.user_groups bll = new BLL.user_groups();
            DataTable dt = bll.GetList(0, strWhere, "grade asc,id asc").Tables[0];

            this.ddlGroupId.Items.Clear();
            this.ddlGroupId.Items.Add(new ListItem("请选择组别...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlGroupId.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }
        }
        #endregion        

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.company bll = new BLL.company();
            Model.company model = bll.GetModel(_id);


            _company = model;

            p = model.province;
            c = model.city;
            a = model.county;

            hidProvince.Value = p;
            hidCity.Value = c;
            hidArea.Value = a;

            txtErpCode.Text = "";
            txtCompanyName.Text = model.companyName;            
            ddlIndustry.SelectedValue = model.industry.ToString();

            txtAddress.Text = model.address;
            rblInvoiceType.SelectedValue = model.invoiceType.ToString();
            txtTaxPoint.Text = model.taxPoint.Trim() == "" ? "6" : model.taxPoint.Trim();
            rblIsPostage.SelectedValue = model.isPostage.ToString();
            txtTel.Text = model.tel;
            txtFax.Text = model.fax;
            txtBankAccount.Text = model.bankAccount;
            txtCompanyCode.Text = model.companyCode;
            txtBankName.Text = model.bankName;
            txtCreateTime.Text = model.createTime.ToString() == "" ? "" : DateTime.Parse(model.createTime.ToString()).ToString("yyyy-MM-dd");
            txtAuthor.Text = model.author;
            txtSl.Text = model.sl.Trim() == "" ? "6" : model.sl.Trim();

            ddlUserTypes.SelectedValue = model.user_type.ToString();

            ddlGroupId.SelectedValue = model.group_id.ToString();
            txtAmount.Text = model.amount.ToString();
            txtOrderLimit.Text = model.order_limit.ToString();
            txtAdd_time.Text = DateTime.Parse(model.add_time.ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            rblStatus.SelectedValue = model.status.ToString();

        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.company model = new Model.company();
            BLL.company bll = new BLL.company();
                       
            model.companyName = txtCompanyName.Text;          
            model.industry = int.Parse(ddlIndustry.SelectedValue);
            model.province = hidProvince.Value;
            model.city = hidCity.Value;
            model.county = hidArea.Value;
            model.address = txtAddress.Text;
            model.invoiceType = int.Parse(rblInvoiceType.SelectedValue);
            model.taxPoint = txtTaxPoint.Text;
            model.isPostage = int.Parse(rblIsPostage.SelectedValue);
            model.tel = txtTel.Text;
            model.fax = txtFax.Text;
            model.bankAccount = txtBankAccount.Text;
            model.companyCode = txtCompanyCode.Text;
            model.bankName = txtBankName.Text;
            if (txtCreateTime.Text != "")
            {
                model.createTime = DateTime.Parse(txtCreateTime.Text);
            }

            model.author = txtAuthor.Text;
            model.sl = txtSl.Text;
            model.group_id = int.Parse(ddlGroupId.SelectedValue);
            model.amount = decimal.Parse(txtAmount.Text);
            model.order_limit = decimal.Parse(txtOrderLimit.Text);
            model.status = int.Parse(rblStatus.SelectedValue);
            model.user_type = int.Parse(ddlUserTypes.SelectedValue);
            model.add_time = txtAdd_time.Text == "" ? DateTime.Now : DateTime.Parse(txtAdd_time.Text);
            model.reg_time = DateTime.Now;
            model.reg_ip = DTRequest.GetIP();
            int newId = bll.Add(model);

            if (newId > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加会员 " + JsonConvert.SerializeObject(model)); //记录日志  
                result = true;
            }

            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.company bll = new BLL.company();
            Model.company model = bll.GetModel(_id);

            int status = model.status;
                       
            model.companyName = txtCompanyName.Text;            
            model.industry = int.Parse(ddlIndustry.SelectedValue);

            model.province = hidProvince.Value;
            model.city = hidCity.Value;
            model.county = hidArea.Value;

            model.address = txtAddress.Text;
            model.invoiceType = int.Parse(rblInvoiceType.SelectedValue);
            model.taxPoint = txtTaxPoint.Text;
            model.isPostage = int.Parse(rblIsPostage.SelectedValue);
            model.tel = txtTel.Text;
            model.fax = txtFax.Text;
            model.bankAccount = txtBankAccount.Text;
            model.companyCode = txtCompanyCode.Text;
            model.bankName = txtBankName.Text;
            if (txtCreateTime.Text != "")
            {
                model.createTime = DateTime.Parse(txtCreateTime.Text);
            }
            model.author = txtAuthor.Text;
            model.sl = txtSl.Text;
            model.group_id = int.Parse(ddlGroupId.SelectedValue);

            model.amount = decimal.Parse(txtAmount.Text);
            model.order_limit = decimal.Parse(txtOrderLimit.Text);
            model.status = int.Parse(rblStatus.SelectedValue);
            model.user_type = int.Parse(ddlUserTypes.SelectedValue);

            if (bll.Update(model, !(model.status == status), ((GetAdminInfo().role_id > 1) ? 0 : 1)))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改会员 " + JsonConvert.SerializeObject(model)); //记录日志
                result = true;
            }

            return result;
        }
        #endregion


        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("user_company", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改会员成功！", "company_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("user_company", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加会员成功！", "company_list.aspx");
            }
        }

        #region 账号管理


        #region 返回用户状态=============================
        protected string GetUserStatus(int status)
        {
            string result = string.Empty;
            switch (status)
            {
                case 0:
                    result = "正常";
                    break;
                case 1:
                    result = "禁用";
                    break;
            }
            return result;
        }
        #endregion

        #region 数据绑定=================================
        private void userRptBind()
        {
            int totalCount = 0;
            int types = DTRequest.GetQueryInt("types");
            int status = DTRequest.GetQueryInt("status");
            string keywords = DTRequest.GetQueryString("keywords");

            string _strWhere = "id>0 and parent_id=" + id + CombSqlTxt(types, status, keywords);
            string _orderby = "reg_time desc,id desc";

            if (types != 0)
            {
                ddlType.SelectedValue = types.ToString();
            }
            if (status != 0)
            {
                ddlStatus.SelectedValue = status.ToString();
            }

            this.txtKeywords.Text = keywords;
            BLL.users bll = new BLL.users();
            this.rptList.DataSource = bll.GetListView(100, 1, _strWhere, _orderby, out totalCount);
            this.rptList.DataBind();
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(int _types, int _status, string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            if (_types > 0)
            {
                strTemp.Append(" and group_id=" + (_types - 1));
            }
            if (_status > 0)
            {
                strTemp.Append(" and status=" + (_status - 1));
            }

            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (companyName like '%" + _keywords + "%' or webchat like '%" + _keywords + "%' or user_name like '%" + _keywords + "%' or mobile like '%" + _keywords + "%' or email like '%" + _keywords + "%' or nick_name like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("user_company", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.users bll = new BLL.users();
            Model.users users = new Model.users();

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    users = bll.GetModel(id);

                    if (users != null && bll.Delete(id))
                    {

                        AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除账号 " + JsonConvert.SerializeObject(users)); //记录日志

                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }


            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("company_edit.aspx",
                "id={0}&action={1}&pageindex={2}&types={3}&status={4}&keywords={5}", id.ToString(), action.ToString(), "2", ddlType.SelectedValue, ddlStatus.SelectedValue, txtKeywords.Text));
        }
        #endregion

        #region 跳转

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            redirect();
        }

        private void redirect()
        {
            Response.Redirect(Utils.CombUrlTxt("company_edit.aspx", "id={0}&action={1}&pageindex={2}&types={3}&status={4}&keywords={5}", id.ToString(), action.ToString(), "2", ddlType.SelectedValue, ddlStatus.SelectedValue, txtKeywords.Text));
        }
        #endregion

        #endregion

        #region 地址管理

        #region 数据绑定=================================
        private void addressRptBind()
        {
            int totalCount = 0;
            string keywords = DTRequest.GetQueryString("keywords");

            string _strWhere = "id>0 and user_id=" + id + addressCombSqlTxt(keywords);
            string _orderby = "is_default desc, id desc";

            this.txtKeywords.Text = keywords;
            BLL.user_addr_book bll = new BLL.user_addr_book();
            this.rptAddress.DataSource = bll.GetList(100, 1, _strWhere, _orderby, out totalCount);
            this.rptAddress.DataBind();
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string addressCombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();

            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (post_code like '%" + _keywords + "%' or accept_name like '%" + _keywords + "%' or area like '%" + _keywords + "%' or address like '%" + _keywords + "%' or mobile like '%" + _keywords + "%' or email like '%" + _keywords + "%' or telphone like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 删除
        protected void btnDeleteAddress_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("user_company", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.user_addr_book bll = new BLL.user_addr_book();
            Model.user_addr_book user_Addr_Book = new Model.user_addr_book();

            for (int i = 0; i < rptAddress.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptAddress.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptAddress.Items[i].FindControl("chkAddressId");
                if (cb.Checked)
                {
                    user_Addr_Book = bll.GetModel(id);

                    if (user_Addr_Book != null && bll.Delete(id))
                    {
                        AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除地址 " + JsonConvert.SerializeObject(user_Addr_Book)); //记录日志

                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }

            JscriptMsg("地址删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("company_edit.aspx",
                "id={0}&action={1}&pageindex={2}&types={3}&status={4}&keywords={5}", id.ToString(), action.ToString(), "1", ddlType.SelectedValue, ddlStatus.SelectedValue, txtAddressKey.Text));
        }
        #endregion

        #region 跳转

        protected void btnAddressFind_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("company_edit.aspx", "id={0}&action={1}&pageindex={2}&types={3}&status={4}&keywords={5}", id.ToString(), action.ToString(), "1", ddlType.SelectedValue, ddlStatus.SelectedValue, txtAddressKey.Text));
        }


        #endregion

        #endregion

        #region 发票管理

        #region 设置分页数量
        private int GetPageSizeInvoice(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("company_invoice_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }

        protected void txtPageNumInvoice_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNumInvoice.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("company_invoice_page_size", "Donal.XmsPage", _pagesize.ToString(), 14400);
                }
            }
            InvoiceRedirect();
        }
        #endregion

        #region 数据绑定=================================
        private void invoiceRptBind()
        {
            int type = DTRequest.GetQueryInt("types");
            int status = DTRequest.GetQueryInt("status");

            this.page = DTRequest.GetQueryInt("page", 1);
            string keywords = DTRequest.GetQueryString("keywords");

            string _strWhere = "erp_code ='" + "" + "' " + invoiceCombSqlTxt(keywords);
            string _orderby = "add_time desc,id desc";

            this.txtInvoiceKey.Text = keywords;

            BLL.user_invoice_log bll = new BLL.user_invoice_log();
            this.rptInvoice.DataSource = bll.GetList(pageSize, page, _strWhere, _orderby, out totalCount);
            this.rptInvoice.DataBind();

            //绑定页码
            txtPageNumInvoice.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("company_edit.aspx", "id={0}&action={1}&pageindex={2}&keywords={3}&page={4}",
                id.ToString(), action.ToString(), "3", txtInvoiceKey.Text, "__id__");
            PageContentInvoice.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string invoiceCombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();

            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (company like '%" + _keywords + "' or erp_code like '%" + _keywords + "%' or user_group like '%" + _keywords + "%' or invoice_title like '%"
                    + _keywords + "%' or invoice_tax_no like '%" + _keywords + "%' or invoice_type like '%" + _keywords + "%' or invoice_no like '%" + _keywords
                    + "%' or express_title like '%" + _keywords + "%' or express_no like '%" + _keywords + "%' or remark like '%" + _keywords + "%')");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 删除
        protected void btnDeleteInvoice_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("user_company", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.user_invoice bll = new BLL.user_invoice();
            Model.user_invoice user_Invoice = new Model.user_invoice();

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkInvoiceId");
                if (cb.Checked)
                {
                    user_Invoice = bll.GetModel(id);

                    if (user_Invoice != null && bll.Delete(id))
                    {
                        AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除发票抬头 " + JsonConvert.SerializeObject(user_Invoice)); //记录日志
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }


            JscriptMsg("发票抬头删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("company_edit.aspx",
                "id={0}&action={1}&pageindex={2}&types={3}&status={4}&keywords={5}", id.ToString(), action.ToString(), "3",
                ddlType.SelectedValue, ddlStatus.SelectedValue, txtInvoiceKey.Text));
        }
        #endregion
        protected void btnInvoiceFind_Click(object sender, EventArgs e)
        {
            InvoiceRedirect();
        }
        #region 跳转

        private void InvoiceRedirect()
        {
            Response.Redirect(Utils.CombUrlTxt("company_edit.aspx", "id={0}&action={1}&pageindex={2}&keywords={3}",
                id.ToString(), action.ToString(), "3", txtInvoiceKey.Text));
        }

        #endregion

        protected void ddlInvoice_SelectedIndexChanged(object sender, EventArgs e)
        {
            InvoiceRedirect();
        }

        protected void ddlInvoiceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            InvoiceRedirect();
        }

        #endregion

        #region 消费记录

        public string GetInoviceStatus(string status)
        {
            string _title = "不开票";

            switch (status)
            {
                case "1":
                    _title = "不开票";
                    break;
                case "2":
                    _title = "未开票";
                    break;
                case "3":
                    _title = "待开票";
                    break;
                case "4":
                    _title = "部分开票";
                    break;
                case "5":
                    _title = "已开票";
                    break;
            }

            return _title;
        }

        #region 设置分页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("company_recode_page_size", "Donal.XmsPage"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("company_recode_page_size", "Donal.XmsPage", _pagesize.ToString(), 14400);
                }
            }
            RecodeRedirect();
        }
        #endregion

        #region 数据绑定=================================
        private void recodeRptBind()
        {
            int types = DTRequest.GetQueryInt("types");
            ddlCashFrom.SelectedValue = _cashFrom.ToString();

            this.page = DTRequest.GetQueryInt("page", 1);
            string keywords = DTRequest.GetQueryString("keywords");

            string _strWhere = " flow_category=0 and company_code !='' and company_code = '" + "" + "' " + recodeCombSqlTxt(keywords);
            string _orderby = "add_time desc,id desc";

            txtRecodeKey.Text = keywords;

            BLL.cash_flow bll = new BLL.cash_flow();
            this.rptRecode.DataSource = bll.GetList(pageSize, page, _strWhere, _orderby, out totalCount);
            this.rptRecode.DataBind();

            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("company_edit.aspx", "id={0}&action={1}&pageindex={2}&cashFrom={3}&keywords={4}&page={5}",
                id.ToString(), action.ToString(), "4", this._cashFrom.ToString(), keywords, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);

        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string recodeCombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (company_code like '%" + _keywords + "%' or company like '%" + _keywords + "%' or order_no like '%" + _keywords + "%' or operators like '%" + _keywords + "%' or cash_middle like '%" + _keywords + "%')");
            }
            switch (_cashFrom)
            {
                case 1:
                    strTemp.Append(" and cash_from='账户充值'");
                    break;
                case 2:
                    strTemp.Append(" and cash_from='财务修改'");
                    break;
                case 3:
                    strTemp.Append(" and cash_from='网上付款'");
                    break;
                case 4:
                    strTemp.Append(" and cash_from='交易退款'");
                    break;
                case 5:
                    strTemp.Append(" and cash_from='营销活动'");
                    break;
                case 6:
                    strTemp.Append(" and cash_from='购买商品'");
                    break;
            }

            return strTemp.ToString();
        }
        #endregion

        #region 删除
        protected void btnDeleteRecode_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("user_company", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.user_invoice bll = new BLL.user_invoice();
            Model.user_invoice user_Invoice = new Model.user_invoice();

            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkInvoiceId");
                if (cb.Checked)
                {
                    user_Invoice = bll.GetModel(id);
                    if (user_Invoice != null && bll.Delete(id))
                    {
                        AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除发票抬头 " + JsonConvert.SerializeObject(user_Invoice)); //记录日志

                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }


            JscriptMsg("发票抬头删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("company_edit.aspx",
                "id={0}&action={1}&pageindex={2}&types={3}&status={4}&keywords={5}", id.ToString(), action.ToString(), "3", ddlType.SelectedValue, ddlStatus.SelectedValue, txtInvoiceKey.Text));
        }
        #endregion

        #region 跳转

        protected void btnRecodeFind_Click(object sender, EventArgs e)
        {
            RecodeRedirect();
        }

        private void RecodeRedirect()
        {
            Response.Redirect(Utils.CombUrlTxt("company_edit.aspx", "id={0}&action={1}&pageindex={2}&cashFrom={3}&keywords={4}",
                id.ToString(), action.ToString(), "4", ddlCashFrom.SelectedValue, txtRecodeKey.Text));
        }
        #endregion

        protected void ddlRecodeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            RecodeRedirect();
        }

        protected void ddlCashFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            RecodeRedirect();
        }
        #endregion

        #region 发票管理

        #region 数据绑定=================================
        private void invRptBind()
        {
            int totalCount = 0;
            string keywords = DTRequest.GetQueryString("keywords");

            string _strWhere = " user_id=" + id + invCombSqlTxt(keywords);
            string _orderby = "is_default desc, id desc";

            this.txtKeywords.Text = keywords;
            BLL.user_invoice bll = new BLL.user_invoice();
            this.rptInv.DataSource = bll.GetList(100, 1, _strWhere, _orderby, out totalCount);
            this.rptInv.DataBind();
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string invCombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (company like  '%" + _keywords + "%' or registration like '%"
                    + _keywords + "%' or opening_bank like '%" + _keywords + "%' or account_number like '%"
                    + _keywords + "%' or telphone like '%" + _keywords + "%' or registered_address like '%" + _keywords + "%')");
            }

            return strTemp.ToString();
        }
        #endregion

        #region 删除
        protected void btnDeleteInv_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("user_company", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.user_invoice bll = new BLL.user_invoice();
            Model.user_invoice user_Invoice = new Model.user_invoice();

            for (int i = 0; i < rptInv.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptInv.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptInv.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    user_Invoice = bll.GetModel(id);

                    if (user_Invoice != null && bll.Delete(id))
                    {
                        AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除发票抬头 " + JsonConvert.SerializeObject(user_Invoice)); //记录日志
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }


            JscriptMsg("地址删除成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("company_edit.aspx",
                "id={0}&action={1}&pageindex={2}&types={3}&status={4}&keywords={5}",
                id.ToString(), action.ToString(), "5", ddlType.SelectedValue, ddlStatus.SelectedValue, txtAddressKey.Text));
        }
        #endregion

        #region 跳转

        protected void btnInvAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("company_edit.aspx", "id={0}&action={1}&pageindex={2}&types={3}&status={4}&keywords={5}",
                id.ToString(), action.ToString(), "5", ddlType.SelectedValue, ddlStatus.SelectedValue, txtAddressKey.Text));
        }


        #endregion

        #endregion

    }
}